package com.example.faas.controller;

import com.example.faas.model.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yanzhao
 * @since 2021/6/11
 */
@RestController
@RequestMapping("/test")
public class TestController {

    ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();

    @GetMapping("get")
    public Object get(String id) {
        if (StringUtils.isBlank(id)) {
            return Response.fail("id can't be null");
        }
        return Response.success(cache.get(id));
    }

    @PostMapping("post")
    public Object post(@RequestBody Map<String, Object> objectMap) {
        if (objectMap == null || objectMap.get("id") == null) {
            return Response.fail("params can't be null");
        }
        cache.put(objectMap.get("id").toString(), objectMap);
        return Response.success();
    }

    @DeleteMapping("/clear")
    public Object clear() {
        cache.clear();
        return Response.success();
    }
}
